package org.example.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.annotation.Log;
import org.example.domain.dto.DTO;
import org.example.domain.po.*;
import org.example.domain.vo.ErpDepotHeadPurchaseVo;
import org.example.domain.vo.ErpDepotItemVO;
import org.example.enums.BusinessType;
import org.example.service.ErpDepotHeadService;
import org.example.service.ErpDepotItemService;
import org.example.service.ErpRoleService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/purchase/order")
public class ErpPurchaseController {

    @Autowired
    private ErpDepotHeadService erpDepotHeadService;

    @Autowired
    private ErpDepotItemService erpDepotItemService;

    @Autowired
    private ErpRoleService erpRoleService;

    /**
     * 采购分页模糊查询
     * @param purchase
     * @return
     */
    @PostMapping("/PurchaseFindByPage")
    public Result<Page<ErpDepotHeadPurchaseVo>> PurchaseFindByPage(@RequestBody Purchase purchase, @RequestAttribute ErpUser user){
        Page<ErpDepotHeadPurchaseVo> page = new Page<>(purchase.getCurrent(), purchase.getSize());
        page = erpDepotHeadService.PurchaseFindByPage(page, purchase, user.getTenantId());
        ErpRole role = erpRoleService.getRoleByUserId(user.getId());
        if (StrUtil.contains("4", role.getPriceLimit())){
            List<String> names = Arrays.asList("totalPrice","discountLastMoney","waitMoney","changeAmount","debt");
            page.getRecords().forEach(item -> {
                DTO.shieldPrice(item, names);
            });
        }
        return Result.success(page);
    }

    /**
     * 采购添加
     * @param depotRequest
     * @return
     */
    @Log(operation = "采购订单添加", businessType = BusinessType.INSERT, isSaveRequestData = true, paramName = "user", jsonPath = "$.username")
    @PostMapping("/insert")
    public Result<String> insert(@RequestBody DepotRequest depotRequest,
                                 @RequestAttribute("user") ErpUser user){
        int i = erpDepotHeadService.insertPurchase(depotRequest, user);
        return i > 0 ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 根据主表id查子表详情
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result<List<ErpDepotItemVO>> findById(@PathVariable("id") Integer id, @RequestAttribute ErpUser user){
        Long tenantId = user.getTenantId();
        List<ErpDepotItemVO> erpDepotItems = erpDepotItemService.findById(id,tenantId);
        ErpRole role = erpRoleService.getRoleByUserId(user.getId());
        if (StrUtil.contains("4", role.getPriceLimit())){
            List<String> names = Arrays.asList("unitPrice","allPrice","taxMoney","taxLastMoney");
            erpDepotItems.forEach(item -> {
                DTO.shieldPrice(item, names);
            });
        }
        return Result.success(erpDepotItems);
    }

    /**
     * 关联查询子单据
     * @param headerId
     * @return
     */
    @GetMapping("/findItems")
    public Result<List<ErpDepotItemVO>> findItems(@RequestParam("headerId") Long headerId){
        List<ErpDepotItemVO> erpDepotItems = erpDepotItemService.findItems(headerId);
        return Result.success(erpDepotItems);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public Result<String> remove(@PathVariable("id") Long id){
        boolean b = erpDepotHeadService.removeById(id);
        return b ? Result.success("删除成功") : Result.success("删除失败");
    }

    /**
     * 批量修改status
     * @param modify
     * @return
     */
    @PutMapping("/modifyTheState")
    public Result<String> modifyTheState(@RequestBody UpdateStatusRequest modify){
        boolean theState = erpDepotHeadService.modifyTheState(modify.getIds(), modify.getStatus());
        return theState ? Result.success("操作成功！") : Result.success("操作失败！");
    }
}
